/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterLineDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterLinePageRequest;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigCaseApprovalCenterLineMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigCaseApprovalCenterLine;
import com.icinfo.cloud.provider.punish.config.service.IConfigCaseApprovalCenterLineService;
import com.icinfo.cloud.provider.punish.config.vo.ConfigCaseApprovalCenterLineVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 描述: 联合法制审核配置参数表-修改审批条线 config_case_approval_center_line 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年04月02日
 */
@Service
public class ConfigCaseApprovalCenterLineServiceImpl extends MyBatisServiceSupport implements IConfigCaseApprovalCenterLineService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigCaseApprovalCenterLineServiceImpl.class);

    @Autowired
    private ConfigCaseApprovalCenterLineMapper configCaseApprovalCenterLineMapper;

    /**
     * 联合法制审核参数配置分页列表
     * @author liyafeng
     * @date 2022/4/2
      * @param request
     * @return {@link List< ConfigCaseApprovalCenterLineVo>}
     */
    @Override
    public List<ConfigCaseApprovalCenterLineVo> getConfigCaseApprovalCenterLineVoList(ConfigCaseApprovalCenterLinePageRequest request) {
        String[] territoryCodeArr = request.getTerritoryCodeArr();
        if(ObjectUtils.isEmpty(territoryCodeArr) || territoryCodeArr.length == 0){
            //解析territoryCode
            String territoryCode = request.getTerritoryCode();
            if(!ObjectUtils.isEmpty(territoryCode)){
                territoryCodeArr = territoryCode.split(Constant.STRING_COMMA);
                request.setTerritoryCodeArr(territoryCodeArr);
            }
        }

        PageHelper.startPage(request.getPageNum(), request.getLength(), request.getOrderBy());
        List<ConfigCaseApprovalCenterLineVo> configCaseApprovalCenterLineVos = configCaseApprovalCenterLineMapper.selectConfigCaseApprovalCenterLineVoList(request);
        return configCaseApprovalCenterLineVos;
    }
    /**
     * 修改审批条线
     * @author liyafeng
     * @date 2022/4/2
      * @param configCaseApprovalCenterLineDto
     * @return {@link int}
     */
    @Override
    public int editConfigCaseApprovalCenterLineDto(ConfigCaseApprovalCenterLineDto configCaseApprovalCenterLineDto) {
        int num=0;
        UserDTO userDTO= UserUtil.getUserInfo();
        //先查询是否存在 不存在则新增 存在则更新
        //todo
        Example example=new Example(ConfigCaseApprovalCenterLine.class);
        example.createCriteria().andEqualTo("configId",configCaseApprovalCenterLineDto.getConfigId()).andEqualTo("reasonNodeCode",configCaseApprovalCenterLineDto.getReasonNodeCode());
        List<ConfigCaseApprovalCenterLine> configCaseApprovalCenterLines=configCaseApprovalCenterLineMapper.selectByExample(example);
        //存在则更新
        if(null!=configCaseApprovalCenterLines&&configCaseApprovalCenterLines.size()>0){
            ConfigCaseApprovalCenterLine updateConfigCaseApprovalCenterLine=new ConfigCaseApprovalCenterLine();
            updateConfigCaseApprovalCenterLine.setId(configCaseApprovalCenterLines.get(0).getId());
            updateConfigCaseApprovalCenterLine.setApprovalTerritoryCode(configCaseApprovalCenterLineDto.getApprovalTerritoryCode());
            updateConfigCaseApprovalCenterLine.setApprovalTerritoryName(configCaseApprovalCenterLineDto.getApprovalTerritoryName());
            updateConfigCaseApprovalCenterLine.setUpdateTime(new Date());
            updateConfigCaseApprovalCenterLine.setUpdateUserId(userDTO.getUserId().toString());
            updateConfigCaseApprovalCenterLine.setUpdateUserName(userDTO.getRealName());
            num=configCaseApprovalCenterLineMapper.updateByPrimaryKeySelective(updateConfigCaseApprovalCenterLine);
            return num;
        }
        //不存在则新增
        configCaseApprovalCenterLineDto.setUpdateUserName(userDTO.getRealName());
        configCaseApprovalCenterLineDto.setUpdateUserId(userDTO.getUserId().toString());
        configCaseApprovalCenterLineDto.setUpdateTime(new Date());
        configCaseApprovalCenterLineDto.setId(StringUtils.uuid());
        num=configCaseApprovalCenterLineMapper.insert(configCaseApprovalCenterLineDto);
        return num;
    }
}